package com.partterns.reflect;

import java.lang.reflect.InvocationTargetException;

/**
 * 有参的反射构建
 * 反射的有点就是通过配置就可以生成对象，可以解除程序的耦合度，比较灵活，缺点是运行慢
 */
public class ReflectServiceImpl2 {
    private String name;
    public ReflectServiceImpl2(String name){
        this.name=name;
    }
    public void sayHello(){
        System.out.println("Hello,"+name);
    }
    public ReflectServiceImpl2 getInstance(){
        ReflectServiceImpl2 object=null;
        try {
            //实际上就等于object=new ReflectServiceImpl2("张三");
            object=(ReflectServiceImpl2) Class.forName("com.partterns.reflect.ReflectServiceImpl2").getConstructor(String.class).newInstance("张三");
        } catch (InstantiationException|IllegalAccessException|InvocationTargetException|NoSuchMethodException|ClassNotFoundException e) {
            e.printStackTrace();
        }
        return object;
    }
}
